package com.example.goodsass;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.mapping.Environment;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.net.*;
import java.util.Enumeration;

@SpringBootApplication
@MapperScan("com.example.goodsass.mapper")
@Slf4j
public class GoodsassApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(GoodsassApplication.class, args);
        //打印swagger接口文档地址 动态获取IP地址和端口
        // 获取运行端口
        int port = context.getEnvironment().getProperty("server.port", Integer.class, 8080);

        // 获取本机IP地址
        String ip = "";
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface iface = interfaces.nextElement();
                if (iface.isLoopback() || !iface.isUp()) continue;

                Enumeration<InetAddress> addresses = iface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress addr = addresses.nextElement();
                    if (addr instanceof Inet4Address) {
                        ip = addr.getHostAddress();
                        break;
                    }
                }
            }
        } catch (SocketException e) {
            ip = "127.0.0.1";
        }

        // 打印带动态IP和端口的Swagger地址
        log.info("Swagger接口文档地址: http://{}:{}/doc.html", ip, port);

    }

}
